package com.zg.autoflow.web;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.autoflow.req.MakeSealReq;
import com.zg.autoflow.req.SettleWorkStepInstanceReq;
import com.zg.autoflow.service.SettleWorkflowStepInstanceService;
import com.zg.autoflow.vo.SettleWorkflowBeforeStepInstanceVo;
import com.zg.autoflow.vo.SettleWorkflowStepInstanceVo;
import com.zg.common.core.R;
import com.zg.common.core.dao.BaseEntity;
import com.zg.common.core.dao.auth.AuthUser;
import com.zg.common.core.dao.auth.AuthUserDao;
import com.zg.common.core.dao.autoflow.dao.SettleWorkflowInstanceDao;
import com.zg.common.core.dao.autoflow.dao.SettleWorkflowInstanceLinkDao;
import com.zg.common.core.dao.autoflow.dao.SettleWorkflowStepInstUserDao;
import com.zg.common.core.dao.autoflow.dao.SettleWorkflowStepInstanceDao;
import com.zg.common.core.dao.autoflow.entity.SettleWorkflowInstance;
import com.zg.common.core.dao.autoflow.entity.SettleWorkflowInstanceLink;
import com.zg.common.core.dao.autoflow.entity.SettleWorkflowStepInstUser;
import com.zg.common.core.dao.autoflow.entity.SettleWorkflowStepInstance;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author zg
*/
@Api(tags = {"环节实例"})
@RestController
@RequiredArgsConstructor
public class SettleWorkflowStepInstanceController {
	private final SettleWorkflowStepInstanceDao settleWorkflowStepInstanceDao;
	private final SettleWorkflowInstanceDao settleWorkflowInstanceDao;
	private final SettleWorkflowStepInstanceService settleWorkflowStepInstanceService;
	private final SettleWorkflowInstanceLinkDao settleWorkflowInstanceLinkDao;
	private final SettleWorkflowStepInstUserDao settleWorkflowStepInstUserDao;
	private final AuthUserDao authUserDao;

	@ApiOperation("查询单个环节实例信息")
	@PostMapping("/workflowManage/getOneStepInstanceByStepInstCode")
	public R<SettleWorkflowStepInstanceVo> getStepInstanceByStepInstCode(@RequestBody String stepInstCode) {
		SettleWorkflowStepInstance obj = settleWorkflowStepInstanceDao.getOne(new LambdaQueryWrapper<SettleWorkflowStepInstance>().eq(SettleWorkflowStepInstance::getStepInstCode, stepInstCode));
		SettleWorkflowStepInstanceVo settleWorkflowStepInstanceVo = new SettleWorkflowStepInstanceVo();
		BeanUtil.copyProperties(obj, settleWorkflowStepInstanceVo);
		return R.success(settleWorkflowStepInstanceVo);
	}

	@ApiOperation("查询当前所有环节实例信息")
	@PostMapping("/workflowManage/getAllStepInstanceByStepInstCode")
	public R<List<SettleWorkflowStepInstanceVo>> getAllStepInstanceByStepInstCode(@RequestBody @Valid SettleWorkStepInstanceReq stepInstanceReq) {
		List<SettleWorkflowStepInstance> list = settleWorkflowStepInstanceDao.list(new LambdaQueryWrapper<SettleWorkflowStepInstance>()
				.eq(SettleWorkflowStepInstance::getInstCode,stepInstanceReq.getInstCode()).orderByAsc(BaseEntity::getCreateTime));
		List<SettleWorkflowStepInstanceVo> settleWorkflowStepInstanceVos = BeanUtil.copyToList(list, SettleWorkflowStepInstanceVo.class);
		if(CollectionUtil.isNotEmpty(settleWorkflowStepInstanceVos)){
			List<SettleWorkflowStepInstUser> stepInstUsers = settleWorkflowStepInstUserDao.list(new LambdaQueryWrapper<SettleWorkflowStepInstUser>()
					.eq(SettleWorkflowStepInstUser::getFlowInstCode, stepInstanceReq.getInstCode()).eq(SettleWorkflowStepInstUser::getDeleteState,"0"));
			List<String> userIds = stepInstUsers.stream().map(SettleWorkflowStepInstUser::getUserId).distinct().collect(Collectors.toList());
			List<AuthUser> authUsers = authUserDao.listByIds(userIds);
			Map<String, String> userMap = authUsers.stream().collect(Collectors.toMap(line->{return line.getId()+"";}, AuthUser::getName));
			Map<String, String> stringStringMap = stepInstUsers.stream().collect(Collectors.toMap(SettleWorkflowStepInstUser::getStepInstCode, SettleWorkflowStepInstUser::getUserId));
			settleWorkflowStepInstanceVos.forEach(stepInstance -> {
				String userId = stringStringMap.get(stepInstance.getStepInstCode());
				String userName = userMap.get(userId);
				stepInstance.setUserId(userId);
				stepInstance.setUserName(userName);
			});
		}
		return R.success(settleWorkflowStepInstanceVos);
	}

	@ApiOperation("查询前置所有环节实例信息")
	@PostMapping("/workflowManage/getAllBeforeStepInstanceByStepInstCode")
	public R<List<SettleWorkflowBeforeStepInstanceVo>> getAllBeforeStepInstanceByStepInstCode(@RequestBody @Valid SettleWorkStepInstanceReq stepInstanceReq) {
		List<SettleWorkflowBeforeStepInstanceVo> settleWorkflowBeforeStepInstanceVos = new ArrayList<>();
		List<SettleWorkflowInstanceLink> beforeInst = settleWorkflowInstanceLinkDao.list(new LambdaQueryWrapper<SettleWorkflowInstanceLink>()
				.eq(SettleWorkflowInstanceLink::getToInstCode, stepInstanceReq.getInstCode()).eq(SettleWorkflowInstanceLink::getDeleteState,"0"));
		beforeInst.forEach(instanceLink->{
			SettleWorkflowBeforeStepInstanceVo settleWorkflowBeforeStepInstanceVo = new SettleWorkflowBeforeStepInstanceVo();
			String fromInstCode = instanceLink.getFromInstCode();
			SettleWorkflowInstance instance = settleWorkflowInstanceDao.getOne(new LambdaQueryWrapper<SettleWorkflowInstance>().eq(SettleWorkflowInstance::getInstCode, fromInstCode));
			List<SettleWorkflowStepInstance> list = settleWorkflowStepInstanceDao.list(new LambdaQueryWrapper<SettleWorkflowStepInstance>()
					.eq(SettleWorkflowStepInstance::getInstCode,fromInstCode).orderByAsc(BaseEntity::getCreateTime));
			List<SettleWorkflowStepInstanceVo> settleWorkflowStepInstanceVos = BeanUtil.copyToList(list, SettleWorkflowStepInstanceVo.class);
			settleWorkflowBeforeStepInstanceVo.setStepInstanceVos(settleWorkflowStepInstanceVos);
			settleWorkflowBeforeStepInstanceVo.setFlowCode(instance.getFlowCode());
			settleWorkflowBeforeStepInstanceVo.setFlowName(instance.getFlowName());
			settleWorkflowBeforeStepInstanceVo.setInstCode(instance.getInstCode());
			settleWorkflowBeforeStepInstanceVos.add(settleWorkflowBeforeStepInstanceVo);
		});
		return R.success(settleWorkflowBeforeStepInstanceVos);
	}

	@ApiOperation("签章")
	@PostMapping("/workflowManage/makeSeal")
	public R makeSeal(@RequestBody MakeSealReq makeSealReq) {
		settleWorkflowStepInstanceService.makeSeal(makeSealReq);
		return R.success();
	}
}
